package com.xiaoshu.controller;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.xiaoshu.bean.Response;
import com.xiaoshu.bean.UserBean;
import com.xiaoshu.param.LoginParam;
import com.xiaoshu.security.IngoreSecurity;
import com.xiaoshu.security.TokenManager;

/**
 * 
 * @author Administrator
 *
 */
@RestController
public class UserController {

	private static final Logger logger = LoggerFactory.getLogger(UserController.class);

	@Autowired
	private TokenManager tokenManager;

	@IngoreSecurity
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public Response login(@Valid LoginParam param) {
		logger.info("用户登录...");
		// 获取登录信息
		String username = param.getUsername();
		String password = param.getPassword();
		// 调用登录服务
		boolean result = username.equals(password);
		if (result) {
			// 创建 token
			String token = tokenManager.createToken(username);
			// 封装 user 对象
			UserBean userBean = new UserBean();
			userBean.setToken(token);
			userBean.setUsername(username);
			// 登录成功，将 user 对象返回前端
			return new Response().success(userBean);
		} else {
			// 登录失败，返回失败信息
			return new Response().failure("login_failure");
		}
	}
}
